package mobi.hami.huapin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.SortDTO;
import mobi.hami.huapin.model.FoodSort;
import mobi.hami.huapin.model.Product;
import mobi.hami.huapin.model.base.PageVO;
import mobi.hami.huapin.service.FoodSortService;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/shot",produces = "application/json")
@Api(description = "商品分类接口api")
public class FoodSortController {
    @Autowired
    private FoodSortService foodSortService;
    @RequestMapping( value = "/save.do",method = RequestMethod.POST)
    @ApiOperation(value = "保存商品分类", notes = " ")
    public Result add(@RequestBody FoodSort foodSort)
    {
        int rs = foodSortService.add(foodSort);
        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping( value = "/getLevelList.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据分类等级获取数据", notes = " ")
    public Result getLevelList(Integer level)
    {
        return Results.successWithData(   foodSortService.getLevelList(level), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping( value = "/getList.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据分类等级获取数据", notes = " ")
    public Result getList(String id,Integer pageindex, Integer pagesize)
    {
       List<FoodSort> list=    foodSortService.getLevelList(1);
       List<FoodSort> list1=    foodSortService.getLevelList(2);
       List<FoodSort> list2=    foodSortService.getLevelList(3);
       List< SortDTO> dtoList=new ArrayList<SortDTO>();
       List< SortDTO  >children= new ArrayList<SortDTO>();
       List< SortDTO  >childrenone= new ArrayList<SortDTO>();
       int size=    pagesize*(pageindex-1);
       int count =list.size();
        List<FoodSort> resList=list.subList(size,(count- size)>=pagesize?pagesize:(count- size));
        for (FoodSort food:resList) {
            SortDTO dto=new SortDTO();
            dto.setId(food.getId());
            dto.setTitle(food.getSortname());
            dto.setIndeterminate(true);
            dto.children=new ArrayList<SortDTO>();
            //一级下面的分类
            for (FoodSort fo:list1)
            {
                  if(food.getId()==fo.getParentid())
                  {
                      SortDTO   dto1=new    SortDTO();
                      dto1.setId(fo.getId());
                      dto1.setTitle(fo.getSortname());
                      dto1.children=new ArrayList<SortDTO>();
                      for (FoodSort chi:list2)
                      {
                          if(fo.getId()==chi.getParentid()){
                              SortDTO    dto2=new    SortDTO();
                              dto2.setId(chi.getId());
                              dto2.setTitle(chi.getSortname());
                              dto1.children.add(dto2);
                          }

                      }
                      dto.children.add(dto1);
                  }

            }
            dtoList.add(dto);
        }
        Map<String,Object > map=new HashMap<String,Object>();
        map.put("total",count);
        map.put("data",dtoList);
        return Results.successWithData(  map, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping( value = "/deleteTree.do",method = RequestMethod.POST)
    @ApiOperation(value = "删除分类", notes = " ")
    public Result deleteTree(String ids)
    {
        return Results.successWithData(   foodSortService.deleteTree(ids), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
}
